/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import utilitarios.ConnectionHelper;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.swing.JOptionPane;
import pojo.Turma;

/**
 *
 * @author gu
 */
public class DaoTurma implements DAO<Turma> {

    private Connection conn = null;
    private PreparedStatement pstm = null;
    private ResultSet rs = null;
    Turma turma;
    String sql;

    public DaoTurma() {
        conn = ConnectionHelper.getIntance().getConnection();
    }
    
    
    
    @Override
    public void commit(Turma beans) throws SQLException {
        sql = "insert into turma (tu_descricao,tu_turno) values (?,?)";
        pstm = conn.prepareStatement(sql);
        
        pstm.setString(1, beans.getDescricao());
        pstm.setString(2, beans.getTurno());
        
        pstm.execute();
    }

    @Override
    public Turma findByName(String name) throws SQLException {
        sql = "select tu_id, tu_descricao, tu_turno from turma where tu_descricao LIKE ?"; 
        pstm = conn.prepareStatement(sql);
        
        pstm.setString(1, name);
        pstm.execute();
        rs = pstm.getGeneratedKeys();
        
        turma.setId(rs.getLong("tu_id"));
        turma.setDescricao(rs.getString("tu_descricao"));
        turma.setTurno(rs.getString("tu_turno"));
        
        return turma;
    }

    @Override
    public void delete(int id) {
        try {
            sql = "delete from turma where al_id = ?";
            pstm = conn.prepareStatement(sql);
            
            pstm.setLong(1, id);
            pstm.execute();
        } catch (SQLException ex) {
            JOptionPane.showMessageDialog(null, "Erro ao deletar Curso!");
        }
    }

    @Override
    public void update(Turma entidade) throws SQLException {
        sql = "UPDATE turma SET tu_descricao = ?, tu_turno = ? WHERE tu_id = ?";
        pstm = conn.prepareStatement(sql);
        
        pstm.setString(1, entidade.getDescricao());
        pstm.setString(3, entidade.getTurno());
        pstm.setLong(4, entidade.getId());
        pstm.execute();
    }
    
}
